/*
 * Copyright 2018 - YZTC
 * 
 * YZTC
 * 本公司保留所有下述内容的权利。
 * 本内容为保密信息，仅限本公司内部使用。
 * 非经本公司书面许可，任何人不得外泄或用于其他目的。
 */
package com.wxmp.wxapi.process;
 
import com.wxmp.core.util.BeanUtil;
import com.wxmp.core.util.HttpClientUtils;
import com.wxmp.core.util.HttpsRequest;
import com.wxmp.core.util.PayUtils;
import com.wxmp.core.util.wx.SignUtil;
import com.wxmp.wxcms.domain.RedGroupPack;
import com.wxmp.wxcms.domain.RedPack;
import com.wxmp.wxcms.service.impl.RedpackServiceImpl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; 
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLContext; 
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;  
/** 
 * @ClassName:WxApiPay 
 * @Function: TODO ADD FUNCTION
 * @Reason:   TODO ADD REASON
 * @Date:     2019年3月5日 下午11:55:05 
 * @author   luck 
 * @version   
 * @since    JDK 1.8
 * @see       
 */
public class WxApiPay {
    
    private static String SEND_REDPACK_URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
    private static String SEND_GROUP_REDPACK_URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";

    private static Logger log = LogManager.getLogger(RedpackServiceImpl.class);

    private  SSLContext getSSLContext(String certLocalPath,String certPassword) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException {
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            FileInputStream instream = new FileInputStream(new File(certLocalPath));//加载本地的证书进行https加密传输
            try {
                keyStore.load(instream, certPassword.toCharArray());//设置证书密码
            } catch (CertificateException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } finally {
                instream.close();
            }
            SSLContext sslcontext = SSLContexts.custom()
                    .loadKeyMaterial(keyStore, certPassword.toCharArray())
                    .build();
            return sslcontext; 
    }
    public  String doSendredpack(String certFile,String password,String secret,RedPack redPack) throws Exception {
        Map<String,Object> param = redPack.toMap();
        log.info("param的值:   " + param.toString());
        String sign = PayUtils.getSign(param) + "key=" + secret;
        log.info("未加密sign的值:   " + sign);
        sign = PayUtils.MD5Encode(sign);
        log.info("加密sign的值:   " + sign);
        param.put("sign", sign);
         
        HttpsRequest hr = new HttpsRequest(certFile,password);
        String result = hr.sendPostString(SEND_REDPACK_URL, PayUtils.map2xml(param,"xml"));
        //.sendPost(SEND_REDPACK_URL, param);
        //SSLContext context  = getSSLContext(certFile,secret);
        //result = HttpClientUtils.sendXmlRequest(SEND_REDPACK_URL, redPack, context);
        return result;
    }
    public  String doSendredpack(String certFile,String password,String secret,RedGroupPack redPack) throws Exception {

        Map<String,Object> param = redPack.toMap();
        log.info("param的值:   " + param.toString());
        String sign = PayUtils.getSign(param) + "key=" + secret;
        log.info("未加密sign的值:   " + sign);
        sign = PayUtils.MD5Encode(sign);
        log.info("加密sign的值:   " + sign);
        param.put("sign", sign);
         
        HttpsRequest hr = new HttpsRequest(certFile,password);
        String result = hr.sendPostString(SEND_GROUP_REDPACK_URL, PayUtils.map2xml(param,"xml"));
//        HttpsRequest hr = new HttpsRequest(certFile,password);
//        String result = hr.sendPost(SEND_GROUP_REDPACK_URL, redPack);
        //SSLContext context  = getSSLContext(certFile,pwd);
        //return HttpClientUtils.sendXmlRequest(SEND_GROUP_REDPACK_URL, redPack, context);
        return result;
    }
}
